import {useEffect, useState} from "react";

const useClock=()=>{
  const [date, setDate] = useState(new Date());
  useEffect(() => {
    console.log("date effect");
    //只需要在didMount时候执⾏就可以了了
    const timer = setInterval(() => {
      setDate(new Date());
    }, 1000);
    //清除定时器器，类似willUnmount
    return () => clearInterval(timer);
  }, []);
  return date;
}
export default useClock;

/*
{useClock().toTimeString()}*/
